package com.ruoyi.loan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.loan.entity.User;
import com.ruoyi.loan.mapper.UserMapper;
import com.ruoyi.loan.service.AdminStudentService;
import com.ruoyi.loan.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author: 廖振辉
 * Date: 2022/4/5
 * Description:
 */
@Service
public class AdminStudentServiceImpl implements AdminStudentService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void resetStudentPassword(String idCard) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id_card", idCard));
        if (user == null){
            throw new RuntimeException("身份证号不存在");
        }
        String userIdCard = user.getIdCard();
        if (userIdCard == null || userIdCard.length() < PasswordUtils.MIN_PASSWORD_LENGTH){
            throw new RuntimeException("身份证号不存在");
        }
        String password = userIdCard.substring(userIdCard.length() - PasswordUtils.MIN_PASSWORD_LENGTH);
        System.out.println("password = " + password);
        String salt = PasswordUtils.getEncryptionSalt();
        String encryptionPassword = PasswordUtils.getEncryptionPassword(password, salt);
        user.setSalt(salt);
        user.setPassword(encryptionPassword);
        userMapper.updateById(user);
    }

}
